package Controlor;

import Model.DAO.uselessDao.AdminDao;
import Model.Entity.Admins;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/pwdchangecheck")
public class CheckPwd extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String oldpwd = req.getParameter("oldpwd");
        String newpwd = req.getParameter("newpwd");
        String newpwdagain = req.getParameter("newpwdagain");
        Admins admin = new Admins();
        admin.setName(req.getSession().getAttribute("admin").toString());
        admin.setPwd(oldpwd);
        int rst = new AdminDao().searchAdmin(admin);
        boolean flag = (rst==1)&&(newpwd.equals(newpwdagain));
        if (flag) {
            admin.setPwd(newpwd);
            new AdminDao().changePwd(admin);
            //转到系统主界面
            req.setAttribute("insertinfo",4);
            req.getRequestDispatcher("Tip.jsp").forward(req, resp);
        } else {
            req.setAttribute("insertinfo",5);
            req.getRequestDispatcher("Tip.jsp").forward(req, resp);//转到错误页
        }
    }
}
